@ECHO OFF
:Initialize
SETLOCAL

WHOAMI /GROUPS | FINDSTR /C:"s-1-16-12288" /I > NUL
IF %ERRORLEVEL% == 1 (
    ECHO Please run this command in an elevated environment.
    GOTO PauseAndRet
)

SET InstallRoot=%~dp0
SET SciTERoot=%InstallRoot:\install\=%
SET Func=:ReplaceFile

IF /I "%1" == "Revert" (
    SET Func=:RevertFile
)

IF NOT EXIST "%SciTE_HOME%\SciTE.exe" (
    SETX SciTE_HOME "%SciTERoot%" /M
)

IF /I "%PROCESSOR_ARCHITECTURE%" == "AMD64" (
    Call %Func% "%SciTERoot%\install\x64\notepad.exe" "%SystemRoot%\notepad.exe" "%SystemRoot%\notepad_scite64.exe"
    Call %Func% "%SciTERoot%\install\x64\notepad.exe" "%SystemRoot%\System32\notepad.exe" "%SystemRoot%\System32\notepad_scite64.exe"
    Call %Func% "%SciTERoot%\install\x86\notepad.exe" "%SystemRoot%\SysWOW64\notepad.exe" "%SystemRoot%\SysWOW64\notepad_scite64.exe"
) else (
    Call %Func% "%SciTERoot%\install\x86\notepad.exe" "%SystemRoot%\notepad.exe" "%SystemRoot%\notepad_scite64.exe"
    Call %Func% "%SciTERoot%\install\x86\notepad.exe" "%SystemRoot%\System32\notepad.exe" "%SystemRoot%\System32\notepad_scite64.exe"
)

ECHO done

:PauseAndRet
PAUSE
:InitializeRet
ENDLOCAL&GOTO :EOF

:ReplaceFile
SETLOCAL
SET Src=%~1
SET Dest=%~2
SET Bak=%~3
IF EXIST "%Src%" (
    IF EXIST "%Dest%" (
        TAKEOWN /F "%Dest%"
        ICACLS "%Dest%" /grant Administrators:F
        IF NOT EXIST "%Bak%" (
            COPY "%Dest%" "%Bak%" /Y
        )
        COPY "%Src%" "%Dest%" /Y
    )
)
ENDLOCAL&GOTO :EOF

:RevertFile
SETLOCAL
SET TF=%~1
SET Dest=%~2
SET Src=%~3
IF EXIST "%Src%" (
    COPY "%Src%" "%Dest%" /Y
    DEL "%Src%" > Nul
)
ENDLOCAL&GOTO :EOF